// pages/expore/index.js
import {$http} from '../../utils/http'
// import fly from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list:[],
        tabArr:[
            {icon:'renwen',title:'正念冥想'},
            {icon:'yejing',title:'睡眠助手'},
            {icon:'yinle',title:'舒缓声音'},
            {icon:'renwen',title:'正念冥想'},
            {icon:'yejing',title:'睡眠助手'},
            {icon:'yinle',title:'舒缓声音'},
        ],
        tabIdx:0, //控制一级菜单
        sId:"",
        cateList:[],
        scrollLeft:0,
        TabCur:0 //控制二级菜单
    },
    handleDetail(ev){
        let {idx,num} = ev.currentTarget.dataset
        console.log(ev,idx,num);
        // 打开新页面
        wx.navigateTo({
          url: `/pages/detail/index?idx=${idx}&num=${num}`,
        })
    },
    //一级菜单
    handleTab(ev){
        let {idx} = ev.currentTarget.dataset
        this.setData({
            tabIdx:idx,
            sId:`tab${idx-1}`,
            TabCur:0
        })
        this.fetchData()
    },
    //二级菜单
    tabSelect(e) {
        this.setData({
          TabCur: e.currentTarget.dataset.id,
          scrollLeft: (e.currentTarget.dataset.id-1)*60
        })
        this.fetchData()
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('探索的onLoad');
        getApp().fly.get('classes/ReactCategory').then(res=>{
            let {results} = res.data
            let parentArr = results.filter(item=>item.parentId=='0-0')
            parentArr.forEach(item=>{
                let children = results.filter(child=>child.parentId==item.objectId)
                children.unshift({
                    name:'全部'
                })
                item.children = children
            })
            console.log(parentArr);
            this.setData({
                cateList:parentArr
            })
        })

        getApp().fly.get('classes/ReactCourse',{
            limit:10
        }).then(res=>{
            this.setData({
                list:res.data.results
            })
        })

        
    },
    fetchData(){
        let {cateList,tabIdx,TabCur} = this.data
        let cateLv1 = cateList[tabIdx].name //一级
        let where = {cateLv1}
        let {children} = cateList[tabIdx]
        if(children[TabCur].name!='全部'){
            where.cateLv2 = children[TabCur].name //二级
            console.log(where);
        }
        getApp().fly.get('classes/ReactCourse',{
            where
        }).then(res=>{
            wx.stopPullDownRefresh() //关闭下拉刷新动画
            this.setData({
                list:res.data.results
            })
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log('下拉刷新');
        this.setData({
            tabIdx:0,
            TabCur:0,
            sId:'tab0',
        })
        this.fetchData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('触底了');
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})